package com.biz.primus.ms.commodity.service.backend.searchRecommendWord;

import com.biz.primus.base.vo.PageableRequestVo;
import com.biz.primus.commodity.vo.backend.BootstrapTablePageResult;
import com.biz.primus.commodity.vo.backend.searchRecommendWord.SearchRecommendWordVo;
import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.SearchRecommendWord;
import com.biz.primus.ms.commodity.dao.redis.searchRecommendWord.SearchRecommendWordRedisDao;
import com.biz.primus.ms.commodity.dao.repository.searchRecommendWord.SearchRecommendWordRepository;
import com.biz.primus.ms.commodity.dao.ro.SearchRecommendWordRo;
import com.biz.primus.ms.commodity.trans.searchRecommendword.SearchRecommendWord2SearchRecommendWordRo;
import com.biz.primus.ms.commodity.trans.searchRecommendword.SearchRecommendWordRo2SearchRecommendWordVo;
import com.biz.primus.ms.commodity.trans.searchRecommendword.SearchRecommendWordVo2SearchRecommendWord;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by aaa on 2017/6/6.
 */
@Service
public class SearchRecommendWordBackendService extends AbstractBaseService {

    @Autowired
    private SearchRecommendWordRedisDao searchRecommendWordRedisDao;

    @Autowired
    private SearchRecommendWordRepository searchRecommendWordRepository;

    public BootstrapTablePageResult<SearchRecommendWordVo> findAll(PageableRequestVo pageRequest) {
        BootstrapTablePageResult<SearchRecommendWordVo> pageResult = new BootstrapTablePageResult<>();
        final List<SearchRecommendWordRo> wordRos = searchRecommendWordRedisDao.findAll();
        pageResult.setRows(Lists.transform(wordRos, new SearchRecommendWordRo2SearchRecommendWordVo()));
        pageResult.setTotal(wordRos.size());
        return pageResult;
    }

    @Transactional
    public void save(SearchRecommendWordVo vo) throws Exception {
        final List<String> idxs = searchRecommendWordRedisDao.getIdxs();
        if (StringUtils.isEmpty(vo.getWordId())) {
            vo.setWordId(String.valueOf(idService.getNextId()));
        }
        SearchRecommendWord searchRecommendWord = new SearchRecommendWordVo2SearchRecommendWord().apply(vo);
        if (StringUtils.isNotEmpty(vo.getCreateTime())) {
            final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final Date date = format.parse(vo.getCreateTime());
            searchRecommendWord.setCreateTimestamp(new Timestamp(date.getTime()));
        } else {
            searchRecommendWord.setCreateTimestamp(DateUtil.now());
        }
        searchRecommendWord.setUpdateTimestamp(DateUtil.now());
        saveOrUpdateUsingPo(searchRecommendWordRepository, searchRecommendWordRedisDao, searchRecommendWord, new SearchRecommendWord2SearchRecommendWordRo());
    }

    public SearchRecommendWordVo findOne(Long id) throws Exception {
        final SearchRecommendWordRo wordRo = searchRecommendWordRedisDao.findOne(id);
        if (wordRo == null) {
            throw new NullPointerException("搜索的推荐词不存在");
        }
        return new SearchRecommendWordRo2SearchRecommendWordVo().apply(wordRo);
    }

    @Transactional
    public void delete(Long id) {
        final SearchRecommendWordRo wordRo = searchRecommendWordRedisDao.findOne(id);
        if (wordRo != null) {
            searchRecommendWordRepository.delete(id);
            this.delayer.executeAfterTransactionCommit(() -> {
                searchRecommendWordRedisDao.delete(wordRo);
            });
        }
    }

    public List<SearchRecommendWordVo> findAll() {
        return Lists.transform(searchRecommendWordRedisDao.findAll(), new SearchRecommendWordRo2SearchRecommendWordVo());
    }
}
